# coding: utf-8

import os
import time
import logging
from django.shortcuts import render_to_response
from django.http import HttpResponse

IS_DEV = os.environ['SERVER_SOFTWARE'].startswith('Dev')  # Development server

def get_rev_number():
    current_path = os.path.dirname(__file__)
    version_file_path = os.path.dirname(current_path)+os.path.sep+'VERSION'
    rev_number = 'NO REV'
    if os.path.exists(version_file_path):
        h = open(version_file_path, 'r')
        rev_number = 'Rev.'+h.read()
        h.close()
#    logging.info('rev_number:'+rev_number)
    return rev_number
    
def ezkeypad_main(request):
    context = {
        'is_dev':IS_DEV,
        'rev_number':get_rev_number(),
    }
    return render_to_response('main.html', context)

def ezkeypad_menifest(request):
    menifest_template = '''
{
  "betaManifestVersion": 1,
  "version": "%s",
  "entries": [
    { "url": "/ezkeypad/media/keypad.png" },
    { "url": "/ezkeypad/media/b.png" },
    { "url": "/ezkeypad/media/stroketable.js" },
    { "url": "/ezkeypad/media/main.js" },
    { "url": "/ezkeypad/media/ime.js" },
    { "url": "/ezkeypad/media/gears_init.js" },
    { "url": "/ezkeypad/media/ez_util.js" },
    { "url": "/ezkeypad/media/ez_jquery.js" },
    { "url": "/ezkeypad/media/ez_gears.js" },
    { "url": "/ezkeypad/media/ez_datastore.js" },
    { "url": "/ezkeypad/media/icons.gif" },
    { "url": "/ezkeypad/media/bg.gif" },
    { "url": "/ezkeypad/media/reset-fonts-grids.css" },
    { "url": "/ezkeypad/media/main.css" },
    { "url": "/ezkeypad/media/icon128.png" },
    { "url": "/ezkeypad/media/icon48.png" },
    { "url": "/ezkeypad/media/icon32.png" },
    { "url": "/ezkeypad/media/icon16.png" },
    { "url": "/ezkeypad/media/ZeroClipboard.js" },
    { "url": "/ezkeypad/media/ZeroClipboard.swf" },
    { "url": "/" }
  ]
}
    ''' % (get_rev_number())
    return HttpResponse(menifest_template)

def ezkeypad_heartbeat(request):
#    time.sleep(3);
    return HttpResponse('OK')

#-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*
# For Slow Test Only
#-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*

SLEEP_INTERVAL = 2 # seconds
SLOW_RES_PATH = os.path.dirname(__file__)+os.path.sep+'slow'+os.path.sep

def ezkeypad_slow_main(request):
    context = {}
    return render_to_response('slow_main.html', context)

def slow_main_css(request):
    h = open(SLOW_RES_PATH+'main.css', 'r')
    buf = h.read()
    h.close()
#    logging.info('start to sleep %d seconds.' % SLEEP_INTERVAL)
#    time.sleep(SLEEP_INTERVAL)
    return HttpResponse(buf)

def slow_ime_js(request):
    h = open(SLOW_RES_PATH+'ime.js', 'r')
    buf = h.read()
    h.close()
#    logging.info('start to sleep %d seconds.' % SLEEP_INTERVAL)
#    time.sleep(SLEEP_INTERVAL+5)
    return HttpResponse(buf)

def slow_bg_png(request):
    h = open(SLOW_RES_PATH+'bg.png', 'rb')
    buf = h.read()
    h.close()
    logging.info('start to sleep %d seconds.' % SLEEP_INTERVAL)
    time.sleep(SLEEP_INTERVAL)
    return HttpResponse(buf, mimetype='image/png')
#-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*
# For Slow Test Only - END
#-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*-~*
